// 封装导航守卫
import router from '@/router'
import store from './store'
const whileList = ['/login', '/404']// 放行白名单
router.beforeEach(async(to, from, next) => {
  const token = store.getters.token
  const url = to.path
  //   如果有token，去登录页=》去首页
  if (token && url === '/login') {
    console.log(1)
    next('/')
  }
  // 如果有token，去其他页=》放行
  if (token && url !== '/login') {
    if (!store.getters.userId) {
      await store.dispatch('user/getUserInfo')
    }
    next()
  }
  // 如果无token，去其他页面=》去登录页
  if (!token && !whileList.includes(url)) {
    console.log(3)
    next('/login')
  }
  // 如果无token，去白名单里面的=》放行
  if (!token && whileList.includes(url)) {
    console.log(url)
    console.log(4)
    next()
  }
})

